/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package panel;

import database.DataEngine;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author daz
 */
public class AddCpPanel extends Screen {

    private JLabel syntaxlLabel;
    private JLabel cpNameLabel;
    private JTextField syntaxlInputField;
    private JTextField cpInputField;
    private JLabel cpIdLabel;
    private JTextField cpIdInputField;
    private JLabel numberActivateLabel;
    private JTextField numberActivateInputField;
    private JLabel chargeMoney1Label;
    private JTextField chargeMoney1InputField;
    private JLabel chargeMoney2Label;
    private JTextField chargeMoney2InputField;
    private JLabel chargeMoney3Label;
    private JTextField chargeMoney3InputField;
    private JLabel chargeMoneyRatioLabel;
    private JTextField chargeMoneyRatioInputField;
    private JButton addCpButton;
    private ActionListener addCpListenner;
    private JButton editCpButton;
    private ActionListener editCpListenner;
    private JButton deleteButton;
    private ActionListener deleteCpListener;
    private DataEngine dataEngine = DataEngine.getInstance();
    private JTextField cost1InputField;
    private JLabel cost1Label;
    private JTextField cost2InputField;
    private JTextField cost3InputField;
    private JTextField numberVendorField;
    private JButton buildManyButton;
    private ActionListener buildManyListenner;
    private int cpId;

    public AddCpPanel(JFrame frame) {
        super(frame);
    }

    @Override
    void initValue() {
    }

    @Override
    void initComponent() {
        //cp line
        setLayout(null);
        int left = 10;
        int top = 20;
        int height = 20;
        int width = 100;
        int linePadding = 35;
        cpNameLabel = new JLabel("Tên CP :");
        cpNameLabel.setBounds(left + 30, top, 250, height);
        add(cpNameLabel);
        cpInputField = new JTextField();
        cpInputField.setBounds(left + 130, top, 150, height);
        add(cpInputField);
        cpIdLabel = new JLabel("Id :");
        cpIdLabel.setBounds(left + 300, top, 30, height);
//        add(cpIdLabel);
        cpIdInputField = new JTextField();
        cpIdInputField.setBounds(left + 330, top, 50, height);
//        add(cpIdInputField);
        // sysntax line
        top += linePadding;
        syntaxlLabel = new JLabel("Cú pháp tin nhắn :");
        syntaxlLabel.setBounds(left + 30, top, 250, height);
        add(syntaxlLabel);
        syntaxlInputField = new JTextField();
        syntaxlInputField.setBounds(left + 130, top, 150, height);
        add(syntaxlInputField);
        // activate line
        top += linePadding;
        numberActivateLabel = new JLabel("Đầu số kích hoạt :");
        numberActivateLabel.setBounds(left + 30, top, 250, height);
        add(numberActivateLabel);
        numberActivateInputField = new JTextField();
        numberActivateInputField.setBounds(left + 130, top, 150, height);
        add(numberActivateInputField);
        // charge money 1 line
        top += linePadding;
        chargeMoney1Label = new JLabel("Đầu số nạp xu  :");
        chargeMoney1Label.setBounds(left + 30, top, 250, height);
        add(chargeMoney1Label);

        chargeMoney1InputField = new JTextField();
        chargeMoney1InputField.setBounds(left + 130, top, 150, height);
        add(chargeMoney1InputField);

        cost1Label = new JLabel("Giá :");
        cost1Label.setBounds(left + 290, top, 100, height);
        add(cost1Label);

        cost1InputField = new JTextField();
        cost1InputField.setBounds(left + 320, top, 60, height);
        add(cost1InputField);
        // charge money 2 line
        top += linePadding;
        chargeMoney2Label = new JLabel("Đầu số nạp xu  :");
        chargeMoney2Label.setBounds(left + 30, top, 250, height);
        add(chargeMoney2Label);

        chargeMoney2InputField = new JTextField();
        chargeMoney2InputField.setBounds(left + 130, top, 150, height);

        cost1Label = new JLabel("Giá :");
        cost1Label.setBounds(left + 290, top, 100, height);

        cost2InputField = new JTextField();
        cost2InputField.setBounds(left + 320, top, 60, height);

        // charge money 3 line
        top += linePadding;
        chargeMoney3Label = new JLabel("Đầu số nạp xu  :");
        chargeMoney3Label.setBounds(left + 30, top, 250, height);

        chargeMoney3InputField = new JTextField();
        chargeMoney3InputField.setBounds(left + 130, top, 150, height);

        cost1Label = new JLabel("Giá :");
        cost1Label.setBounds(left + 290, top, 100, height);

        cost3InputField = new JTextField();
        cost3InputField.setBounds(left + 320, top, 60, height);

        // charge money ratio  line
        top += linePadding;
        chargeMoneyRatioLabel = new JLabel("Hệ số nạp xu  :");
        chargeMoneyRatioLabel.setBounds(left + 30, top, 250, height);
        chargeMoneyRatioInputField = new JTextField();
        chargeMoneyRatioInputField.setBounds(left + 130, top, 150, height);

        // add cp button
        top += linePadding + 5;
        addCpButton = new JButton("Thêm cp");
        addCpButton.addActionListener(addCpListenner);
        addCpButton.setBounds(left + 30, top, width - 20, height);

        editCpButton = new JButton("Sửa cp");
        editCpButton.addActionListener(editCpListenner);
        editCpButton.setBounds(left + 130, top, width - 20, height);

        deleteButton = new JButton("Xóa cp");
        deleteButton.setBounds(left + 230, top, width - 20, height);
        deleteButton.addActionListener(deleteCpListener);

        top += linePadding + 5;
        buildManyButton = new JButton("Thêm nhiều cp");
        buildManyButton.addActionListener(buildManyListenner);
        buildManyButton.setBounds(left + 30, top, width + 20, height);
        numberVendorField = new JTextField();
        numberVendorField.setBounds(left + 160, top, width + 20, height);

        add(chargeMoney2InputField);
        add(cost1Label);
        add(cost2InputField);
        add(chargeMoney3Label);
        add(chargeMoney3InputField);
        add(cost1Label);
        add(cost3InputField);
        add(chargeMoneyRatioLabel);
        add(chargeMoneyRatioInputField);
        add(addCpButton);
        add(editCpButton);
        add(deleteButton);
        add(buildManyButton);
        add(numberVendorField);
    }

    private boolean isLegal() {

        if (cpInputField.getText().length() == 0) {
            showInfo("Bạn chưa điền tên cp");
            return false;
        }
        if (syntaxlInputField.getText().length() == 0) {
            showInfo("Bạn chưa điền cú pháp tin nhắn");
            return false;
        }
        if (numberActivateInputField.getText().length() == 0) {
            showInfo("Bạn chưa điền đầu số kích hoạt");
            return false;
        }
        if (chargeMoney1InputField.getText().length() == 0
                || chargeMoney2InputField.getText().length() == 0
                || chargeMoney3InputField.getText().length() == 0) {
            showInfo("Bạn chưa điền đầu số nạp xu");
            return false;
        }
        if (cost1InputField.getText().length() == 0
                || cost2InputField.getText().length() == 0
                || cost3InputField.getText().length() == 0) {
            showInfo("Bạn chưa điền giá Sms");
            return false;
        }
        if (chargeMoneyRatioInputField.getText().length() == 0) {
            showInfo("Bạn chưa điền tỉ lệ");
            return false;
        }
        return true;
    }

    @Override
    void initListenner() {
        addCpListenner = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (isLegal()) {
                    if (dataEngine.isCpCreated(cpInputField.getText()) > 0) {
                        showInfo("Cp này đã được tạo");
                        return;
                    }
                    addNewCp();
                }
            }
        };
        editCpListenner = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (isLegal()) {

                    if (dataEngine.isCpCreated(cpInputField.getText()) == 0) {
                        showInfo("Cp này chưa được tạo");
                        return;
                    }
                    editCp();
                }
            }
        };
        deleteCpListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (cpInputField.getText().length() == 0) {
                    showInfo("Bạn chưa điền tên cp");
                    return;
                }
                if (dataEngine.isCpCreated(cpInputField.getText()) == 0) {
                    showInfo("Cp này không tồn tại");
                    return;
                }
                dataEngine.deleteCp(cpInputField.getText());
                showInfo("Đã xóa thành công cp " + cpInputField.getText());
            }
        };
        buildManyListenner = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (isLegal()) {
                    Thread thread = new Thread(new Runnable() {

                        @Override
                        public void run() {
                            if (!dataEngine.isConnected) {
                                showInfo("Không thể kết nối đến database ");
                                return;
                            }
                            dataEngine.addManyCp(cpInputField.getText(), numberActivateInputField.getText(), syntaxlInputField.getText(), chargeMoney1InputField.getText(),
                                    chargeMoney2InputField.getText(), chargeMoney3InputField.getText(), cost1InputField.getText(), cost2InputField.getText(),
                                    cost3InputField.getText(), chargeMoneyRatioInputField.getText(), Integer.parseInt(numberVendorField.getText()));
                            JOptionPane.showMessageDialog(frame,
                                    "Thêm cp thành công");
                        }
                    });
                    thread.start();
                }
            }
        };
    }

    private void editCp() {
        if (!dataEngine.isConnected) {
            showInfo("Không thể kết nối đến database ");
            return;
        }
        dataEngine.editCp(cpInputField.getText(), numberActivateInputField.getText(), syntaxlInputField.getText(), chargeMoney1InputField.getText(),
                chargeMoney2InputField.getText(), chargeMoney3InputField.getText(), cost1InputField.getText(), cost2InputField.getText(),
                cost3InputField.getText(), chargeMoneyRatioInputField.getText());
        showInfo("Đã sửa thành công thông tin cp");
    }

    private void addNewCp() {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                if (!dataEngine.isConnected) {
                    showInfo("Không thể kết nối đến database ");
                    return;
                }
                cpId = dataEngine.addNewCp(cpInputField.getText(), numberActivateInputField.getText(), syntaxlInputField.getText(), chargeMoney1InputField.getText(),
                        chargeMoney2InputField.getText(), chargeMoney3InputField.getText(), cost1InputField.getText(), cost2InputField.getText(),
                        cost3InputField.getText(), chargeMoneyRatioInputField.getText());
                showDialog(cpId);
            }
        });
        thread.start();
    }

    private void showInfo(String info) {
        JOptionPane.showMessageDialog(frame,
                info);
    }

    private void showDialog(int cpId) {
        JOptionPane.showMessageDialog(frame,
                "Thêm cp thành công. Cp mới là " + cpInputField.getText() + ",có id :" + cpId);
    }
}
